package com.system.controller;

import com.system.po.CourseCustom;
import com.system.po.SelectedCourseCustom;
import com.system.po.Selectedcourse;
import com.system.po.Student;
import com.system.service.CourseService;
import com.system.service.SelectedCourseService;
import com.system.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "教师")
@RequestMapping("/wxteacher")
public class WxTeacherController
{
    @Resource(name = "teacherServiceImpl")
    private TeacherService teacherService;

    @Resource(name = "courseServiceImpl")
    private CourseService courseService;

    @Resource(name = "selectedCourseServiceImpl")
    private SelectedCourseService selectedCourseService;

    @RequestMapping(value = "/wxshowCourse", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> wxshowcourse(String username) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        //   Subject subject = SecurityUtils.getSubject();
        //   String username = (String) subject.getPrincipal();

        List<CourseCustom> list = courseService.findByTeacherID(Integer.parseInt(username));
        map.put("courseList", list);
        return map;
    }

    @RequestMapping(value = "/wxgradeCourse", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> wxgradecourse(Integer courseid) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        if (courseid == null) {
            map.put("teachergrade", "false");
        }
        List<SelectedCourseCustom> list = selectedCourseService.findByCourseID(courseid);
        map.put("selectedCourseList", list);
        return map;
    }

    @RequestMapping(value = "/wxmark1", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> wxmark1(SelectedCourseCustom scc) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        SelectedCourseCustom selectedCourseCustom = selectedCourseService.findOne(scc);

        map.put("selectedCourse", selectedCourseCustom);

        return map;
    }

    @RequestMapping(value = "/wxmark2", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String, Object> wxmark2(SelectedCourseCustom scc) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        selectedCourseService.updataOne(scc);
        map.put("mark", "true");
        return map;
    }

    @ApiOperation(value = "录入多个学生成绩")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/wxCourseMark", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> wxCourseMark(List<Selectedcourse> list, Integer courseId) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        for (Selectedcourse scc: list) {
            scc.setCourseid(courseId);
            selectedCourseService.updateById(scc);
        }
        map.put("mark", "true");
        return map;
    }

    @ApiOperation(value = "录入单个成绩")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/wxCourseMarkOne", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> wxCourseMarkOne(Integer courseId , Integer point ,Integer userid) throws Exception {
        Selectedcourse selectedcourse = new Selectedcourse();
        selectedcourse.setStudentid(userid);
        selectedcourse.setCourseid(courseId);
        selectedcourse.setMark(point);
        Map<String, Object> map = new HashMap<String, Object>();
        selectedCourseService.updateById(selectedcourse);
        map.put("mark", "true");
        return map;
    }

    @ApiOperation(value = "删除单个成绩")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/wxDeleteOne", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> wxDeleteOne(Integer id,Integer studentId) throws Exception {
        Selectedcourse selectedcourse = new Selectedcourse();
        selectedcourse.setMark(0);
        selectedcourse.setCourseid(id);
        selectedcourse.setStudentid(studentId);
        Map<String, Object> map = new HashMap<String, Object>();
        selectedCourseService.updateById(selectedcourse);
        map.put("mark", "true");
        return map;
    }

    @RequestMapping(value = "/getPointByTeachId", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public List<Student> getPointByTeachId(String id){
       return selectedCourseService.getPointByTeachId(id);
    }
}
